<template>
  <el-dialog v-model="dialogVisible" title="修改密码" width="500px" draggable :close-on-click-modal="false">
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px" label-position="right" status-icon>
      <el-form-item label="原密码" prop="oldPassword">
        <el-input v-model="formData.oldPassword" type="password" placeholder="请输入原密码" show-password />
      </el-form-item>

      <el-form-item label="新密码" prop="newPassword">
        <el-input v-model="formData.newPassword" type="password" placeholder="请输入新密码" show-password />
      </el-form-item>

      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input v-model="formData.confirmPassword" type="password" placeholder="请再次输入新密码" show-password />
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleSubmit" :loading="loading"> 确认 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue";
import { ElMessage, type FormInstance, type FormRules } from "element-plus";
import { updatePassword } from "@/api/modules/user";
import { useUserStore } from "@/stores/modules/user";
const userStore = useUserStore();

const dialogVisible = ref(false);
const loading = ref(false);
const formRef = ref<FormInstance>();

const formData = reactive({
  oldPassword: "",
  newPassword: "",
  confirmPassword: ""
});

// 验证确认密码
const validateConfirmPassword = (rule: any, value: string, callback: any) => {
  if (value !== formData.newPassword) {
    callback(new Error("两次输入的密码不一致"));
  } else {
    callback();
  }
};

const rules = reactive<FormRules>({
  oldPassword: [
    { required: true, message: "请输入原密码", trigger: "blur" },
    { min: 6, max: 20, message: "密码长度在6到20个字符", trigger: "blur" }
  ],
  newPassword: [
    { required: true, message: "请输入新密码", trigger: "blur" },
    { min: 6, max: 20, message: "密码长度在6到20个字符", trigger: "blur" },
    {
      validator: (rule, value, callback) => {
        if (value === formData.oldPassword) {
          callback(new Error("新密码不能与原密码相同"));
        } else {
          callback();
        }
      },
      trigger: "blur"
    }
  ],
  confirmPassword: [
    { required: true, message: "请再次输入新密码", trigger: "blur" },
    { validator: validateConfirmPassword, trigger: "blur" }
  ]
});

const openDialog = () => {
  dialogVisible.value = true;
  // 重置表单
  if (formRef.value) {
    formRef.value.resetFields();
  }
};

const handleCancel = () => {
  dialogVisible.value = false;
};

const handleSubmit = async () => {
  if (!formRef.value) return;

  try {
    await formRef.value.validate((valid: boolean) => {
      if (!valid) return;
      loading.value = true;
      updatePassword({
        userId: userStore.userInfo.userId,
        oldPassword: formData.oldPassword,
        newPassword: formData.newPassword
      }).then(() => {
        dialogVisible.value = false;
        ElMessage.success("密码修改成功");
      });
    });
  } catch (error) {
    ElMessage.error("修改密码失败" + error);
  } finally {
    loading.value = false;
  }
};

defineExpose({ openDialog });
</script>

<style scoped>
.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
</style>
